<?php 
	class Manage_customer extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->library('session');
			$this->load->library('form_validation');
			$this->load->library('cismarty');
			$this->load->model("apanel/template");
			$this->load->model("apanel/db_customer");
			$this->load->model("apanel/db_info");
			$this->load->library('pagination');
		}
		
		public function index(){
			redirect("manage_customer/customer_list");
		}
		
		public function customer_list($st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			list($cat,$num,$page,$total) = $this->db_customer->listall();
			$this->cismarty->assign("Num_Rows",$num);
			$this->cismarty->assign("cat",$cat);
			$this->cismarty->assign("pagelink",$page);
			$this->cismarty->assign("total",$total);
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
				
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open('manage_customer/cutomer_delAll'));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/customer/customer_list.html');
		}
		
		public function customer_add(){
			//insert
 			if($this->input->post("bntSubmit") != null){
		 			$this->form_validation->set_rules("name","Name","required");
		 			$this->form_validation->set_rules("cname","Customer Name","trim");
		 			$this->form_validation->set_rules("phone","Phone","trim");
		 			$this->form_validation->set_rules("fax","fax","trim");
		 			$this->form_validation->set_rules("web","web","trim");
		 			$this->form_validation->set_rules("email","Email","trim|valid_email");
		 			$this->form_validation->set_rules("facebook","facebook","trim");
		 			$this->form_validation->set_rules("twitter","twitter","trim");
		 			$this->form_validation->set_rules("blog","blog","trim");
		 			$this->form_validation->set_rules("contact","contact","trim");
		 			$this->form_validation->set_rules("email2","Contact Email","trim|valid_email");
		 			$this->form_validation->set_rules("address1","address1","trim");
		 			$this->form_validation->set_rules("address2","address2","trim");
		 			$this->form_validation->set_value('desc_en','Description En','trim');
		 			$this->form_validation->set_value('desc_cn','Description Cn','trim');
		 			$this->form_validation->set_value('desc_th','Description Th','trim');
		 			
		 			if($this->form_validation->run() == FALSE){
		 				$this->cismarty->assign("name",set_value('name'));
		 				$this->cismarty->assign("cname",set_value('cname'));
		 				$this->cismarty->assign("phone",set_value('phone'));
		 				$this->cismarty->assign("fax",set_value('fax'));
		 				$this->cismarty->assign("web",set_value('web'));
		 				$this->cismarty->assign("email",set_value('email'));
		 				$this->cismarty->assign("facebook",set_value('facebook'));
		 				$this->cismarty->assign("twitter",set_value('twitter'));
		 				$this->cismarty->assign("blog",set_value('blog'));
		 				$this->cismarty->assign("contact",set_value('contact'));
		 				$this->cismarty->assign("email2",set_value('email2'));
		 				$this->cismarty->assign("address1",set_value('address1'));
		 				$this->cismarty->assign("address2",set_value('address2'));
		 				$this->cismarty->assign("desc_en",set_value('desc_en'));
		 				$this->cismarty->assign("desc_cn",set_value('desc_cn'));
		 				$this->cismarty->assign("desc_th",set_value('desc_th'));
		 				
		 				//set error
		 				$this->cismarty->assign("errorname",form_error('name'));
		 				$this->cismarty->assign("erroremail",form_error('email'));
		 				$this->cismarty->assign("erroremail2",form_error('email2'));
		 				
		 			}else{
		 				$error = false;
		 				$picname = "";
		 				if($_FILES['pic']['tmp_name'] != ""){
		 					$config['upload_path'] = 'media/room/';
		 					$config['allowed_types'] = 'gif|jpg|png';
		 					$config['max_size']	= '1024'; // kb
		 					$config['create_thumb'] = TRUE;
		 					$this->load->library('upload', $config);
		 					if ( $this->upload->do_upload('pic'))
		 					{
		 						$data =  $this->upload->data();
		 						$picname = date('YmdHis').$data['file_ext'];
		 						$picnamethumb = date('YmdHis').'_thumb'.$data['file_ext'];
		 						rename($data['full_path'], $data['file_path'].$picname);
		 						$config['image_library'] ='gd2';
		 						$config['source_image'] =  $data['file_path'].$picname;
		 						$config['width']	= 100;
		 						$config['height'] = 70;
		 						$this->load->library('image_lib', $config);
		 						$this->image_lib->resize();
		 						
		 					}else{
		 						//$pic_error = array('error' => $this->upload->display_errors());
		 						$this->cismarty->assign("errorpic",$this->upload->display_errors());
		 						$error = true;
		 					}
		 				}
		 				if(!$error){
			 				$data = array(
			 						"name"=>$this->input->post('name'),
			 						"customer_name"=>$this->input->post('cname'),
			 						"phone"=>$this->input->post('phone'),
			 						"fax"=>$this->input->post('fax'),
			 						"email"=>$this->input->post('email'),
			 						"website"=>$this->input->post('web'),
			 						"address1"=>addslashes($this->input->post('address1')),
			 						"address2"=>addslashes($this->input->post('address2')),
			 						"description_en"=>addslashes($this->input->post('desc_en')),
			 						"description_zh_cn"=>addslashes($this->input->post('desc_cn')),
			 						"description_th"=>addslashes($this->input->post('desc_th')),
			 						"img"=>$picname,
			 						"country_pkid"=>$this->input->post('country'),
			 						"facebook"=>$this->input->post('facebook'),
			 						"twitter"=>$this->input->post('twitter'),
			 						"blog"=>$this->input->post('blog'),
			 						"contact_person"=>$this->input->post('contact'),
			 						"contact_email"=>$this->input->post('email2'),
			 						"created_by"=>$this->session->userdata("sess_username"),
			 						"created_date"=>date('Y-m-d H:i:s'),
			 						);
								$res = $this->db->insert("mobile_customer",$data); 	
								if($res) redirect('manage_customer/customer_list/success');
								else  redirect('manage_customer/customer_list/fail');
			 			}
		 			}
 			}//end post	
 		$this->cismarty->assign("tiny",$this->template->tinymce());
 		//== template
 		$this->cismarty->assign("str_country",$this->db_info->country_list());
 		$this->cismarty->assign("header",$this->template->header());
 		$this->cismarty->assign("menu",$this->template->menu(7));
 		$this->cismarty->assign("footer",$this->template->footer());
 		$this->cismarty->assign("url",base_url());
 		$this->cismarty->assign("form_open",form_open_multipart("manage_customer/customer_add"));
 		$this->cismarty->assign("form_close",form_close());
 		$this->cismarty->display('apanel/customer/customer_add.html');
		}
		
		public function customer_edit($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($this->input->post('bntSubmit') != null){
				$this->form_validation->set_rules("category","Room Category","required");
				$this->form_validation->set_rules("room_no","Room No.","required");
				$this->form_validation->set_rules("startdate","Check In","required");
				$this->form_validation->set_rules("enddate","Check Out","required");
				$this->form_validation->set_rules("name","Name Surname","required");
				$this->form_validation->set_rules("phone","phone","trim");
				$this->form_validation->set_rules("fax","fax","trim");
				$this->form_validation->set_rules("email","email","trim|valid_email");
				$this->form_validation->set_rules("welcome","welcome","trim");
				$this->form_validation->set_rules("address1","address1","trim");
				$this->form_validation->set_rules("address2","address2","trim");
				$this->form_validation->set_rules("country","country","trim");
				$this->form_validation->set_rules("point","point","trim|numeric");
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("phone",set_value('phone'));
					$this->cismarty->assign("fax",set_value('fax'));
					$this->cismarty->assign("email",set_value('email'));
					$this->cismarty->assign("welcome",set_value('welcome'));
					$this->cismarty->assign("address1",set_value('address1'));
					$this->cismarty->assign("address2",set_value('address2'));
					$this->cismarty->assign("point",set_value('point'));
					$this->cismarty->assign("startdate",set_value('startdate'));
					$this->cismarty->assign("enddate",set_value('enddate'));
						
					//set error
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("erroremail",form_error('email'));
					$this->cismarty->assign("errorpoint",form_error('point'));
					$this->cismarty->assign("errorcate",form_error('category'));
					$this->cismarty->assign("errorroom",form_error('room_no'));
					$this->cismarty->assign("errorstartdate",form_error('startdate'));
					$this->cismarty->assign("errorenddate",form_error('enddate'));
				}else {
					$data = array(
							"name"=>trim($this->input->post('name')),
							"phone"=>trim($this->input->post('phone')),
							"fax"=>trim($this->input->post('fax')),
							"email"=>trim($this->input->post('email')),
							"txt_welcome"=>addslashes($this->input->post('welcome')),
							"address1"=>trim($this->input->post('address1')),
							"address2"=>trim($this->input->post('address2')),
							"country_pkid"=>trim($this->input->post('country')),
							"point"=>trim($this->input->post('point')),
							"created_by"=>$this->session->userdata("sess_username"),
							"created_date"=>date("Y-m-d H:i:s")
					);
					$this->db->where("pkid",$id);
					$res = $this->db->update('mobile_customer',$data);
					//== check in
					$arr_chekin = array(
								"room_pkid"=>$this->input->post('room_no'),
							 	"check_in_date"=>$this->input->post('startdate'),
								"check_out_date"=>$this->input->post('enddate')
							);
					$this->db->update("hotel_customer_transaction",$arr_chekin);
					
					if($res) redirect('manage_customer/customer_list/success');
					else  redirect('manage_customer/customer_list/fail');
				}
			
			}else {
				$this->db->where("pkid",$id);
				$res = $this->db->get('hotel_customer');
				$row = $res->row();
				$this->cismarty->assign("name",$row->name);
				$this->cismarty->assign("phone",$row->phone);
				$this->cismarty->assign("fax",$row->fax);
				$this->cismarty->assign("email",$row->email);
				$this->cismarty->assign("welcome",stripslashes($row->txt_welcome));
				$this->cismarty->assign("address1",$row->address1);
				$this->cismarty->assign("address2",$row->address2);
				$this->cismarty->assign("point",$row->point);
				$country = $row->country_pkid;
				$this->db->where("customer_pkid",$id);
				$rowcate = $this->db->get('hotel_customer_transaction')->row();
				$room_id = $room_no = $rowcate->room_pkid;
				$this->cismarty->assign("startdate",$rowcate->check_in_date);
				$this->cismarty->assign("enddate",$rowcate->check_out_date);
				
			}
			$this->load->model("apanel/db_hotel");
			$this->cismarty->assign("tiny",$this->template->tinymce());
			$this->cismarty->assign("str_country",$this->db_hotel->country_list($country));
			$this->load->model("apanel/db_room");
			list($group,$str_room) =$this->db_customer->room_checkin($room_id);
			$this->cismarty->assign("str_cate",$this->db_room->show_cat($group));
			$this->cismarty->assign("str_room",$str_room);
			//== template
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open("manage_customer/customer_edit/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/customer/customer_edit.html');
		}
		
		public function customer_del($id){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($id != ""){
				$this->db->where('pkid',$id);
				$res = $this->db->delete("mobile_customer");
				if($res) redirect('manage_customer/customer_list/success');
			}
		}
		
		public function cutomer_delAll(){
			$check = $this->input->post('check');
			if($check != ""){
				foreach ($check as $index){
					$this->db->where('pkid',$index);
					$res = $this->db->delete("mobile_customer");
				}
			}
			redirect('manage_customer/customer_list/success');
		}
		


	
		public function customer_info($id="",$st=""){
			if($this->session->userdata('sess_login') == null)
				redirect('apanel');
			if($st == "success"){
				$this->cismarty->assign("status","<div class=\"mws-form-message success message\">Success</div>");
			}
			$country = "";
			$this->db->select('pkid');
			$this->db->where("pkid",$id);
			$resrow =$this->db->get('mobile_customer');
			$row = $resrow->row();
			$pkid = $row->pkid;
			//update
			if($this->input->post("bntSubmit") != null){
				$this->form_validation->set_rules("name","Name","required");
				$this->form_validation->set_rules("phone","Phone","trim");
				$this->form_validation->set_rules("fax","fax","trim");
				$this->form_validation->set_rules("web","web","trim");
				$this->form_validation->set_rules("email","Email","trim|valid_email");
				$this->form_validation->set_rules("facebook","facebook","trim");
				$this->form_validation->set_rules("twitter","twitter","trim");
				$this->form_validation->set_rules("blog","blog","trim");
				$this->form_validation->set_rules("contact","contact","trim");
				$this->form_validation->set_rules("email2","Contact Email","trim|valid_email");
				$this->form_validation->set_rules("address1","address1","trim");
				$this->form_validation->set_rules("address2","address2","trim");
				$this->form_validation->set_value('desc_en','Description En','trim');
				$this->form_validation->set_value('desc_cn','Description Cn','trim');
				$this->form_validation->set_value('desc_th','Description Th','trim');
		
				if($this->form_validation->run() == FALSE){
					$this->cismarty->assign("name",set_value('name'));
					$this->cismarty->assign("phone",set_value('phone'));
					$this->cismarty->assign("fax",set_value('fax'));
					$this->cismarty->assign("web",set_value('web'));
					$this->cismarty->assign("email",set_value('email'));
					$this->cismarty->assign("facebook",set_value('facebook'));
					$this->cismarty->assign("twitter",set_value('twitter'));
					$this->cismarty->assign("blog",set_value('blog'));
					$this->cismarty->assign("contact",set_value('contact'));
					$this->cismarty->assign("email2",set_value('email2'));
					$this->cismarty->assign("address1",set_value('address1'));
					$this->cismarty->assign("address2",set_value('address2'));
					$this->cismarty->assign("desc_en",set_value('desc_en'));
					$this->cismarty->assign("desc_cn",set_value('desc_cn'));
					$this->cismarty->assign("desc_th",set_value('desc_th'));
		
					//set error
					$this->cismarty->assign("errorname",form_error('name'));
					$this->cismarty->assign("erroremail",form_error('email'));
					$this->cismarty->assign("erroremail2",form_error('email2'));
		
				}else{
					$error = false;
					if($_FILES['pic']['tmp_name'] != ""){
						$config['upload_path'] = 'media/room/';
						$config['allowed_types'] = 'gif|jpg|png';
						$config['max_size']	= '1024'; // kb
						$config['create_thumb'] = TRUE;
						$this->load->library('upload', $config);
						if ( $this->upload->do_upload('pic'))
						{
							$data =  $this->upload->data();
							$picname = date('YmdHis').$data['file_ext'];
							$picnamethumb = date('YmdHis').'_thumb'.$data['file_ext'];
							rename($data['full_path'], $data['file_path'].$picname);
							$config['image_library'] ='gd2';
							$config['source_image'] =  $data['file_path'].$picname;
							$config['width']	= 350;
							$config['height'] = 89;
							$this->load->library('image_lib', $config);
							$this->image_lib->resize();
							$uppic = array('img'=>$picname);
							$this->db->where("pkid",$id);
							$this->db->update('mobile_customer',$uppic);
						}else{
							//$pic_error = array('error' => $this->upload->display_errors());
							$this->cismarty->assign("errorpic",$this->upload->display_errors());
							$error = true;
						}
					}
					if(!$error){
						$data = array(
								"name"=>$this->input->post('name'),
								"customer_name"=>$this->input->post("cname"),
								"phone"=>$this->input->post('phone'),
								"fax"=>$this->input->post('fax'),
								"email"=>$this->input->post('email'),
								"website"=>$this->input->post('web'),
								"address1"=>$this->input->post('address1'),
								"address2"=>$this->input->post('address2'),
								"description_en"=>addslashes($this->input->post('desc_en')),
								"description_zh_cn"=>addslashes($this->input->post('desc_cn')),
								"description_th"=>addslashes($this->input->post('desc_th')),
								"country_pkid"=>$this->input->post('country'),
								"facebook"=>$this->input->post('facebook'),
								"twitter"=>$this->input->post('twitter'),
								"blog"=>$this->input->post('blog'),
								"contact_person"=>$this->input->post('contact'),
								"contact_email"=>$this->input->post('email2'),
								"updated_by"=>$this->session->userdata("sess_username"),
								"updated_date"=>date('Y-m-d H:i:s'),
						);
						$this->db->where("pkid",$id);
						$res = $this->db->update("mobile_customer",$data);
						if($res) redirect("manage_customer/customer_info/$id/success");
						else  redirect('manage_customer/customer_info/fail');
					}
				}
			}//end post
			else{
				$this->db->where("pkid",$id);
				$res = $this->db->get('mobile_customer');
				$row = $res->row();
				$this->cismarty->assign("name",$row->name);
				$this->cismarty->assign("cname",$row->customer_name);
				$this->cismarty->assign("phone",$row->phone);
				$this->cismarty->assign("fax",$row->fax);
				$this->cismarty->assign("web",$row->website);
				$this->cismarty->assign("email",$row->email);
				$this->cismarty->assign("facebook",$row->facebook);
				$this->cismarty->assign("twitter",$row->twitter);
				$this->cismarty->assign("blog",$row->blog);
				$this->cismarty->assign("contact",$row->contact_person);
				$this->cismarty->assign("email2",$row->contact_email);
				$this->cismarty->assign("address1",stripslashes($row->address1));
				$this->cismarty->assign("address2",stripslashes($row->address2));
				$country = $row->country_pkid;
				$this->cismarty->assign("desc_en",stripslashes($row->description_en));
				$this->cismarty->assign("desc_cn",stripslashes($row->description_zh_cn));
				$this->cismarty->assign("desc_th",stripslashes($row->description_th));
				if(trim($row->img) != "") $this->cismarty->assign("img",'<img src="'.base_url().'media/room/'.$row->img.'"><br>');
			}
		
			$this->cismarty->assign("tiny",$this->template->tinymce());
			//== template
			$this->cismarty->assign("str_country",$this->db_info->country_list($country));
			$this->cismarty->assign("header",$this->template->header());
			$this->cismarty->assign("menu",$this->template->menu(7));
			$this->cismarty->assign("footer",$this->template->footer());
			$this->cismarty->assign("url",base_url());
			$this->cismarty->assign("form_open",form_open_multipart("manage_customer/customer_info/$id"));
			$this->cismarty->assign("form_close",form_close());
			$this->cismarty->display('apanel/customer/info.html');
		}
		
	}
?>